<template>
  <div class="wrapper">
    <div class="form">
      <el-form class="form1" ref="form" :model="model" :rules="rules">
        <el-form-item prop="username">
          <el-input
            v-model="model.username"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="model.password"
            show-password
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="deng">
        <el-button type="primary" :loading="loading" @click="userLogin"
          >登录</el-button
        >
        <el-button type="info" @click="reset">重置</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { login } from "@/api/user";
import { getMenus } from "api/rights";
export default {
  name: "login",
  data() {
    return {
      loading: false,
      model: {},
      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空",
          },
          {
            min: 3,
            max: 10,
            message: "用户名长度3到10个字符",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
          },
          {
            min: 6,
            max: 15,
            message: "密码长度为6到15个字符",
          },
        ],
      },
    };
  },
  methods: {
    reset() {
      this.$refs.form.resetFields();
      this.model = {};
    },
    /**
     * 登录
     */
    async userLogin() {
      try {
        await this.$refs.form.validate();
        this.loading = true;
        const data = await login(this.model);
        this.$store.commit("setUser", data);
        const menus = await getMenus();
        this.$store.commit("setMenus", menus);
        this.$router.push({ name: "HomeLayout" });
      } catch (error) {
        console.log(error);
      }
      this.loading = false;
    },
  },
};
</script>

<style scoped  lang="scss">
.wrapper {
  height: 1000px;
  background: #2b4b6b;
  .form {
    position: relative;
    top: 200px;
    left: 40%;
    width: 450px;
    height: 300px;
    background: white;
    border-radius: 3px;
    padding: 0 20px;
    .form1 {
      padding-top: 100px;
    }
  }
}
</style>